﻿using CommonTools;
using shop.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace shop.daifa
{
    /// <summary> 根据商品编号, 日期, 查询订单明细内容 </summary>
    public partial class DaifaForm : Form
    {
        Bll.daifa.NeedBuyerInfoBll _bll = new Bll.daifa.NeedBuyerInfoBll();

        public DaifaForm()
        {
            InitializeComponent();
        }

        private async void btn_query_Click(object sender, EventArgs e)
        {
            var list = await _bll.FindTrades(this.box_begin.Value, this.box_end.Value, this.box_spec_no.Text.Trim()
                , this.box_src_tids.Text.Trim());


            this.grid.AddItems(list,
                a=> true,
                a => a.id,
                a => a.FTime,
                a => a.ShopName,
                a => a.src_tids,
                a => a.TradeNo,
                a => a.Qty,
                a => a.spec_no,
                a => a.PlatName
                );
        }

        private async void btn_ok_Click(object sender, EventArgs e)
        {
            var list = new List<NeedBuyerInfo>();

            foreach (DataGridViewRow item in grid.Rows)
            {
                if (item.Cells[cl_select.Index].Value.IsNull(false))
                {
                    list.Add(item.Tag as NeedBuyerInfo);
                }
            }

            if (list.Count > 0)
            {
                var res = await WaitUI.RunAsync(async () =>
                {
                    return await _bll.SaveItem(list);
                });

                if(res.IsOk)
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    MessageBox.Show(res.Message, Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("没有选择任何项", Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


        }
    }
}
